package com.hilon.base.service;

import com.hilon.segment.mybatis.utils.ResultBean;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;

import java.util.List;

public abstract class EntityHandle<T> {
    private JpaRepository curJpaRepository;

    protected void buildCurRepository(JpaRepository curJpaRepository) {
        this.curJpaRepository = curJpaRepository;
    }

    public ResultBean<List<T>> findAll() {
        try {
            List<T> list = curJpaRepository.findAll();
            return new ResultBean<>(list);
        } catch (Exception e) {
            return new ResultBean<>(500, "获取所有记录!");
        }
    }

    public ResultBean<List<T>> listEntityByCondition(T entity) {
        try {
            ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                    .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()) //姓名采用“开始匹配”的方式查询
                    .withMatcher("baseNo", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withIgnorePaths("id");  //忽略属性：是否关注。因为是基本类型，需要忽略掉

            //创建实例
            Example<T> example = Example.of(entity, matcher);
            //查询
            List<T> list = curJpaRepository.findAll(example);
            return new ResultBean<>(list);
        } catch (Exception e) {
            return new ResultBean<>(500, "查询错误!");
        }
    }


    public ResultBean<Void> saveEntity(T entity) {
        try {
            curJpaRepository.save(entity);
            return new ResultBean<>(ResultBean.SUCCESS, "新增成功!");
        } catch (Exception e) {
            // 事务回滚, 需要在类或方法上添加注解. @Transactional(rollbackFor = Exception.class)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultBean<>(ResultBean.SQL_CHANGE_WRONG, "程序新增失败!");
        }
    }

    public ResultBean<Void> saveAll(List<T> entities) {
        try {
            curJpaRepository.saveAll(entities);
            return new ResultBean<>(ResultBean.SUCCESS, "批量保存成功!");
        } catch (Exception e) {
            // 事务回滚, 需要在类或方法上添加注解. @Transactional(rollbackFor = Exception.class)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultBean<>(ResultBean.SQL_CHANGE_WRONG, "批量保存失败!");
        }
    }


    public ResultBean<Void> updateEntity(Long id, T entity) {
        T curEntity = (T) curJpaRepository.getReferenceById(id);
        Assert.notNull(curEntity, "部门不存在");
        try {
            curJpaRepository.save(entity);
            return new ResultBean<>(ResultBean.SUCCESS, "编辑成功!");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultBean<>(ResultBean.SQL_CHANGE_WRONG, "程序编辑失败!");
        }
    }


    public ResultBean<Void> deleteBatchEntityById(List<Long> idList) {
        for (Long id : idList) {
            T curEntity = (T) curJpaRepository.getReferenceById(id);
            Assert.notNull(curEntity, "选中了不存在的数据!");
        }
        try {
            curJpaRepository.deleteAllByIdInBatch(idList);
            return new ResultBean<>(ResultBean.SUCCESS, "删除成功!");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultBean<>(ResultBean.SQL_CHANGE_WRONG, "删除失败!");
        }
    }
}
